{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9c9be844-2d13-4b60-83b6-96688a03ccad",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "# 将数据插入字符串\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9c33aaf6-bc24-4fe0-bf93-96c3fc744787",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'James'\n",
    "height = 181.18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "52b50533-091b-4ac6-8d89-e5e7f4ec4dda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.18 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用 + 运算符\n",
    "str_1 = name + ' has a height of ' + str(height) + ' cm.'\n",
    "print(str_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a97ff289",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.18 cm.\n"
     ]
    }
   ],
   "source": [
    "str_11 = name + ' has a height of ' + str(height) + ' cm.'\n",
    "print(str_11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1aba9024-2f82-4db9-9006-93fd904d08fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用 % \n",
    "str_2 = '%s has a height of %.3f cm.' %(name, height)\n",
    "print(str_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f9bebec7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "str_22 = '%s has a height of %.3f cm.' %(name, height)\n",
    "print(str_22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "20edd9f9-9f1f-4bce-9561-a9a326dc2256",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用 str.format()\n",
    "str_3 = '{} has a height of {:.3f} cm.'.format(name, height)\n",
    "print(str_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a2017338-d661-4a29-87fe-b9ce4200c426",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用f-strings\n",
    "str_4 = f'{name} has a height of {height:.3f} cm.'\n",
    "print(str_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "abe892c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "str_44 = f'{name} has a height of {height:.3f} cm.'\n",
    "print(str_44)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df889ebb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
